/*
 * 
 * LegendShop 多用户商城系统
 * 
 *  版权所有,并保留所有权利。
 * 
 */
package com.legendshop.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.legendshop.oa.dto.WeeklyTimeDto;

/**
 * 周报时间工具类.
 */
public class WeeklyTimeUtil {

	/**
	 * 根据当前时间来计算本周的开始时间和结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static WeeklyTimeDto getWeeklyTime(Date date) {
		WeeklyTimeDto dto = new WeeklyTimeDto();
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);// 设置星期一为一周开始的第一天
	    calendar.setMinimalDaysInFirstWeek(7);//可以不用设置
		calendar.setTimeInMillis(date.getTime());// 获得当前的时间戳
		int weekYear = calendar.get(Calendar.YEAR);// 获得当前的年
		int weekNo = calendar.get(Calendar.WEEK_OF_YEAR);// 获得当前日期属于今年的第几周

		// System.out.println("第几周："+weekOfYear);
		calendar.setWeekDate(weekYear, weekNo, 2);// 获得指定年的第几周的开始日期
		long starttime = calendar.getTime().getTime();// 创建日期的时间该周的第一天，
		calendar.setWeekDate(weekYear, weekNo, 1);// 获得指定年的第几周的结束日期
		long endtime = calendar.getTime().getTime();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String startDate = simpleDateFormat.format(starttime);// 将时间戳格式化为指定格式
		String endDate = simpleDateFormat.format(endtime);
		dto.setCurrentDate(date);
		dto.setYear(weekYear);
		if(weekNo < 53){
			dto.setWeekNo(weekNo + 1);//设置为加1,为了跟日志控件兼容
		}else{
			dto.setWeekNo(weekNo);//最后一个月是没有新增数量
		}

		dto.setStartDate(startDate);
		dto.setEndDate(endDate);
		return dto;
	}
	
	public static WeeklyTimeDto getWeeklyTime(Integer year, Integer weekNo) {
		WeeklyTimeDto dto = new WeeklyTimeDto();
		if(year != 0 && weekNo != 0) {
			dto.setYear(year);
			dto.setWeekNo(weekNo);
			dto.setStartDate(getStartDayOfWeekNo(year, weekNo));
			dto.setEndDate(getEndDayOfWeekNo(year, weekNo));
		}
		return dto;
	}


	/**
	 * 获取指定年指定周的开始日期
	 * 
	 * @paramyear
	 * @paramweekNo
	 * @return
	 */
	public static String getStartDayOfWeekNo(int year, int weekNo) {
		Calendar cal = getCalendarFormYear(year);
		cal.set(Calendar.WEEK_OF_YEAR, weekNo);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String monthStr = null;
		String dayStr = null;
		StringBuilder monthBuilder = new StringBuilder();
		StringBuilder dayBuilder = new StringBuilder();
		if (month < 10) {
			monthStr = monthBuilder.append("0").append(month).toString();
		} else {
			monthStr = monthBuilder.append(month).toString();
		}
		if (day < 10) {
			dayStr = dayBuilder.append("0").append(day).toString();
		} else {
			dayStr = dayBuilder.append(day).toString();
		}
		return cal.get(Calendar.YEAR) + "-" + monthStr + "-" + dayStr;

	}

	/**
	 * 获取指定年指定周的结束日期
	 * 
	 * @paramyear
	 * @paramweekNo
	 * @return
	 */
	public static String getEndDayOfWeekNo(int year, int weekNo) {
		Calendar cal = getCalendarFormYear(year);
		cal.set(Calendar.WEEK_OF_YEAR, weekNo);
		cal.add(Calendar.DAY_OF_WEEK, 6);
		int month = cal.get(Calendar.MONTH) + 1;
		int day = cal.get(Calendar.DAY_OF_MONTH);
		String monthStr = null;
		String dayStr = null;
		StringBuilder monthBuilder = new StringBuilder();
		StringBuilder dayBuilder = new StringBuilder();
		if (month < 10) {
			monthStr = monthBuilder.append("0").append(month).toString();
		} else {
			monthStr = monthBuilder.append(month).toString();
		}
		if (day < 10) {
			dayStr = dayBuilder.append("0").append(day).toString();
		} else {
			dayStr = dayBuilder.append(day).toString();
		}
		return cal.get(Calendar.YEAR) + "-" + monthStr + "-" + dayStr;
	}

	/**
	 * 获取指定年份日历
	 * 
	 * @paramyear
	 * @return
	 */
	private static Calendar getCalendarFormYear(int year) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.set(Calendar.YEAR, year);
		return cal;
	}
	
	/**
	 * 根据当前时间来计算本周的开始时间和结束时间
	 * 
	 * @param date
	 * @return
	 */
	public static Integer getCurrentYear(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.MONDAY);// 设置星期一为一周开始的第一天
	    calendar.setMinimalDaysInFirstWeek(6);//可以不用设置
		calendar.setTimeInMillis(date.getTime());// 获得当前的时间戳
		int weekYear = calendar.get(Calendar.YEAR);// 获得当前的年
		return weekYear;
	}

	public static void main(String[] args) {
		Date date = TimerUtil.strToDateShort("2018-12-30");

		System.out.println("date = " + date);
		System.out.println(getWeeklyTime(date));
		
		System.out.println(getStartDayOfWeekNo(2018,52));
		System.out.println(getEndDayOfWeekNo(2018,52));
		
		System.out.println(getCurrentYear(new Date()));
	}



}
